package com.uchiha.nova.core.event;


import com.uchiha.nova.core.feign.ILogClient;
import com.uchiha.nova.core.model.LogUsual;
import com.uchiha.nova.core.props.NovaProperties;
import com.uchiha.nova.core.props.ServerInfo;
import com.uchiha.nova.core.publisher.EventConstant;
import com.uchiha.nova.core.utils.LogAbstractUtil;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author 周林静
 * @Date 2021-10-25 15:19
 * @Version 1.0
 */
@Slf4j
@AllArgsConstructor
@Component
public class UsualLogListener {

    @Resource
    private final ILogClient logService;

    private final ServerInfo serverInfo;

    private final NovaProperties novaProperties;

    @Async
    @Order
    @EventListener(UsualLogEvent.class)
    public void saveUsualLog(UsualLogEvent event) {
        log.info("UsualLogListener-saveUsualLog:{}",event);
        Map<String, Object> source = (Map<String, Object>) event.getSource();
        LogUsual logUsual = (LogUsual) source.get(EventConstant.EVENT_LOG);
        LogAbstractUtil.addOtherInfoToLog(logUsual, novaProperties, serverInfo);
        logService.saveUsualLog(logUsual);
    }

}
